package com.exartisan.utils.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.content.Context;

public class FileUtil {
	public static void saveTextFileToInternalStorage(Context context, String content, String filename){
		BufferedWriter bufferedWriter;
		try {
			bufferedWriter = new BufferedWriter(new FileWriter(new File(
					context.getFilesDir() + File.separator + filename+".txt")));
			bufferedWriter.write(content);
			bufferedWriter.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String loadTextFileFromInternalStorage(Context context, String filename){
		BufferedReader bufferedReader;
		String content = "";
		try {
			bufferedReader = new BufferedReader(new FileReader(
					new File(
							context.getFilesDir() + File.separator + filename+".txt")));
			String read;
			StringBuilder builder = new StringBuilder("");

			while ((read = bufferedReader.readLine()) != null) {
				builder.append(read);
			}
		    content = builder.toString();
			bufferedReader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;
	}
}
